﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class FloorDao
    {


        public FloorDao()
        {

            Mapper.CreateMap<E_Floor, FloorModel>();
        }
        public List<E_Floor> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Floor.Count();

                return edm.E_Floor.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public FloorModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Floor.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Floor, FloorModel>(query);
            }

        }


        public int Save(FloorModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Floor.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Floor();

                    edm.AddToE_Floor(query);

                }


                query.FloorName = obj.FloorName;

                query.PID = obj.PID;

                query.FloorPath = obj.FloorPath;

                query.OrderID = obj.OrderID;
                query.CreateUserId = obj.CreateUserId;
                edm.SaveChanges();

                return query.Id;


            }

        }
        public List<FloorModel> GetFloorByPId(int pid)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                var query = edm.E_Floor.Where(s=>s.PID==pid).ToList();
                return Mapper.Map<List<E_Floor>, List<FloorModel>>(query);
            }
        }

        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Floor.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

